<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="optType === 'add' || optType === 'update'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSelectProduct"
          v-hasPermi="['as:zeroLine:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="zeroLineList"
      border
      @cell-mouse-enter="handleCellEnter()"
      @cell-mouse-leave="handleCellLeave()"
      >
      <el-table-column label="产品物料ID" align="center" prop="itemId" />
      <el-table-column label="产品物料编码" align="center" prop="itemCode" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="数量" align="center" prop="quantity">
        <div class="item" slot-scope="scope">
          <el-input
            class="item_input"
            v-model="scope.row.quantity"
            size="mini"
            style="width: 100%;"
            :title="scope.row.quantity"
            @blur="handleBlur(scope.row)"
            v-if="optType === 'update'"
          />
          <el-input
            class="item_input"
            v-model="scope.row.quantity"
            size="mini"
            style="width: 100%;"
            :title="scope.row.quantity"
            readonly
            v-if="optType !== 'update'"
          />
        </div>
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          v-if="optType === 'add' || optType === 'update'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['as:zeroLine:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ItemSelect ref="itemSelect" @onSelected="onItemSelected" > </ItemSelect>

  </div>
</template>

<script>
import { listZeroLine, getZeroLine, delZeroLine, addZeroLine, updateZeroLine } from "@/api/mes/as/zeroLine";
import ItemSelect  from "../afterSalesWorkOrder/itemSelect.vue";

export default {
  name: "ZeroLine",
  components: {
    ItemSelect
  },
  props: {
    optType: null,
    zeroId: null
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 零公里工单产品明细表格数据
      zeroLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zeroId: this.zeroId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemId: [
          { required: true, message: "产品物料ID不能为空", trigger: "blur" }
        ],
      },
      // 可修改单元格属性
      editProp: ['quantity'],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询零公里工单产品明细列表 */
    getList() {
      this.loading = true;
      listZeroLine(this.queryParams).then(response => {
        this.zeroLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        zeroId: this.zeroId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加零公里工单产品明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getZeroLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改零公里工单产品明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateZeroLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZeroLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除零公里工单产品明细编号为"' + ids + '"的数据项？').then(function() {
        return delZeroLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('as/zeroLine/export', {
        ...this.queryParams
      }, `zeroLine_${new Date().getTime()}.xlsx`)
    },
    handleSelectProduct(){
      this.$refs.itemSelect.showFlag = true;
    },
    //物料选择弹出框
    onItemSelected(obj){
      if(obj !== undefined && obj !== null){
        this.form.zeroId = this.zeroId;
        this.form.itemId = obj.itemId;
        this.form.itemCode = obj.itemCode;
        this.form.itemName = obj.itemName;
        this.form.specification = obj.specification;
        this.form.unitOfMeasure = obj.unitOfMeasure;
        this.form.itemOrProduct = obj.itemOrProduct;
        addZeroLine(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    // 列表单元格鼠标进入事件
    handleCellEnter (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'block'
        }
      }
    },
    // 列表单元格鼠标离开事件
    handleCellLeave (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'none'
        }
      }
    },
    // 列表单元格输入框失去焦点事件
    handleBlur(row) {
      const quantity = parseFloat(row.quantity)
      if (isNaN(quantity) || !Number.isInteger(quantity) || quantity <= 0) {
        this.$modal.msgError("请输入大于0的正整数");
        this.getList();
        return;
      }
      updateZeroLine(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    }
  }
};
</script>
